﻿using AutoMapper;
using B.S.BasicData.Domain.SystemUsers;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Implements;
using B.S.BasicData.Infrastructure.Interfaces;
using B.S.BasicData.Write.Api.Application.Command.SystemUsers.IAMCommand;
using MediatR;

namespace B.S.BasicData.Write.Api.Application.CommandHandler.SystemUsers.IAMCommand
{
    public class IAMShowPageCommandHandler:IRequestHandler<IAMShowPageCommand,ApiPaging<IAM_Model>>
    {
        private readonly IBaseRepository<IAM_Model> _baseRepositoryIAM;
        public IAMShowPageCommandHandler(IBaseRepository<IAM_Model> showbaseRepository)
        {
            _baseRepositoryIAM = showbaseRepository;
        }
        /// <summary>
        /// 显示身份校验
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiPaging<IAM_Model>> Handle(IAMShowPageCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiPaging<IAM_Model>();
            var list = _baseRepositoryIAM.GetAll();
            if (!string.IsNullOrEmpty(request.UserName))
            {
                list = list.Where(m => m.UserName.Contains(request.UserName));
            }
            res.TotalCount = list.Count();
            res.TotalPage = (int)Math.Ceiling(res.TotalCount * 1.0 / request.PageSize);
            res.Items = list.OrderByDescending(m => m.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return Task.FromResult(res);
        }
    }
}
